import axios from 'axios'

export default function ajax (url, data = {}) {
  return new Promise((resolve, reject) => {
    let promise
    let dataStr = '' // 数据拼接字符串
    Object.keys(data).forEach(key => {
      dataStr += `${key}=${data[key]}&`
    })
    if (dataStr !== '') {
      dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
      url = `${url}?${dataStr}`
    }

    // 发送get请求
    promise = axios.get(url)

    promise.then(response => resolve(response.data))
      .catch(error => reject(error))
  })
}
